library(readr)
library(dplyr)
library(DescTools)
library(glue)
library(sf)
library(ggplot2)
library(ggthemes)
library(ggiraph)
library(tidyverse)
library(patchwork)
library(viridis)
library(devtools)
library(zoom)
library(plotly)
dados <- read_csv("cases-brazil-cities-time_2022.csv.gz")
dados <- dados%>%
  filter(state == "RS")%>%
  filter(date > "2022-10-31" & date < "2022-12-01")
dados$ibgeID <- as.character(dados$ibgeID)
total_casosRS<-dados%>%
  group_by(ibgeID)%>%
  summarise(casos_total = sum(newCases), city = Mode(city))
total_casosRS
## # A tibble: 498 x 3
##    ibgeID  casos_total city                            
##    <chr>         <dbl> <chr>                           
##  1 43            -2525 CASO SEM LOCALIZAÇÃO DEFINIDA/RS
##  2 4300034           0 Aceguá/RS                       
##  3 4300059           2 Água Santa/RS                   
##  4 4300109           8 Agudo/RS                        
##  5 4300208          18 Ajuricaba/RS                    
##  6 4300307           1 Alecrim/RS                      
##  7 4300406          71 Alegrete/RS                     
##  8 4300455           0 Alegria/RS                      
##  9 4300471           7 Almirante Tamandaré do Sul/RS   
## 10 4300505           2 Alpestre/RS                     
## # ... with 488 more rows
sf <- st_read("RS_Municipios_2021.shx")
## Reading layer `RS_Municipios_2021' from data source 
##   `C:\Users\TEMP\Downloads\RS_Municipios_2021.shx' using driver `ESRI Shapefile'
## Simple feature collection with 499 features and 4 fields
## Geometry type: MULTIPOLYGON
## Dimension:     XY
## Bounding box:  xmin: -57.64974 ymin: -33.75118 xmax: -49.69135 ymax: -27.0823
## Geodetic CRS:  SIRGAS 2000
sf
## Simple feature collection with 499 features and 4 fields
## Geometry type: MULTIPOLYGON
## Dimension:     XY
## Bounding box:  xmin: -57.64974 ymin: -33.75118 xmax: -49.69135 ymax: -27.0823
## Geodetic CRS:  SIRGAS 2000
## First 10 features:
##     CD_MUN                     NM_MUN SIGLA  AREA_KM2
## 1  4300001                Lagoa Mirim    RS  2872.364
## 2  4300002            Lagoa dos Patos    RS 10196.942
## 3  4300034                     Aceguá    RS  1551.339
## 4  4300059                 Água Santa    RS   291.503
## 5  4300109                      Agudo    RS   534.624
## 6  4300208                  Ajuricaba    RS   322.674
## 7  4300307                    Alecrim    RS   316.394
## 8  4300406                   Alegrete    RS  7800.428
## 9  4300455                    Alegria    RS   172.794
## 10 4300471 Almirante Tamandaré do Sul    RS   265.042
##                          geometry
## 1  MULTIPOLYGON (((-52.62347 -...
## 2  MULTIPOLYGON (((-51.29028 -...
## 3  MULTIPOLYGON (((-54.10842 -...
## 4  MULTIPOLYGON (((-52.04086 -...
## 5  MULTIPOLYGON (((-53.25392 -...
## 6  MULTIPOLYGON (((-53.79538 -...
## 7  MULTIPOLYGON (((-54.77813 -...
## 8  MULTIPOLYGON (((-55.96128 -...
## 9  MULTIPOLYGON (((-54.06037 -...
## 10 MULTIPOLYGON (((-53.00526 -...
sf$nome = paste(sf$NM_MUN, "/", sf$SIGLA, sep="")
sf = sf[,-(1:4)]

mapa <- merge(x = sf, y = total_casosRS, by = intersect("geometry","ibgeID"))
mapa$int = paste(mapa$city, ": ", mapa$casos_total, " casos", sep="")

graf = ggplot(mapa, 
              aes(x = reorder(city, casos_total), 
                  y = casos_total,
                  tooltip = int, data_id = city)) +
              geom_col_interactive(color="darkred", 
                                     fill="gray", 
                                     size=0.5) + 
              theme_calc() +
              theme(axis.text=element_text(size = 1)) +  #<<
              labs(title = "Casos de covid em Novembro de 2022 no RS",
                   subtitle = "Fonte: https://github.com/wcota/covid19br") + 
              ylab("") + 
              xlab("") + 
              coord_flip()


rs = ggplot(data = mapa, aes(fill = casos_total), 
                                 color = "white") +
         geom_sf_interactive(aes(tooltip = int), 
                                 size = .2, color = "black") +
         scale_fill_viridis_c(option = "F", name = "Nº de casos", 
                              direction = -1) +
         labs(x=NULL, y=NULL, 
              title="Mapa dos municípios do RS") +
         theme_calc()


girafe(ggobj = rs + graf,
       width_svg = 10, height_svg = 5) %>% girafe_options(opts_hover(css = "fill:cyan;"))